// import * as THREE from "three"
let THREE=require('three');

class MaterialMgr {


  constructor() {
    console.log("MaterialMgr constructor");
    this.haveTextureMaterialNode = [];
  }


  //无返回Promise方式创建材质
  createMaterial(materialNodes) {

    for (let index = 0; index < materialNodes.length; index++) {
      const materialNode = materialNodes[index];
      let material = null;
      switch (materialNode.MaterialTp) {
        case 0:
          //玻璃
          material = this.createGlass(this.parseColor(materialNode.ColorJson))
          break;
        case 1:
          //贴图
          material = this.createTexture(materialNode.TextureUrl);
          break;
        case 2:
          //纯色
          material = this.createColor(this.parseColor(materialNode.ColorJson));
          break;
        default:
          break;
      }
      this.mapMaterial(materialNode.AttachIDS, material);
    }
  }

  //创建ids和材质的字典
  mapMaterial(ids, material) {
    if (this.materialMap == null) this.materialMap = {};
    for (let index = 0; index < ids.length; index++) {
      const id = ids[index];
      this.materialMap[id] = material;
    }
  }

  //根据ids获取创建好的材质
  getMaterial(id) {
    let material = this.materialMap[id];
    return material;
  }

  //解析颜色
  parseColor(colorJson) {
    return new THREE.Color(colorJson.R, colorJson.G, colorJson.B);
  }


  //创建铝材材质
  createAluminium(url){

    let loader = new THREE.TextureLoader();
    let material = new THREE.MeshPhongMaterial({
      color: new THREE.Color(0.5254901960784314,0.5254901960784314,0.5254901960784314),
      side : THREE.DoubleSide,
      roughness : 0,
      metalness : 0,
      opacity :1,
      shininess : 0,
      specular : new THREE.Color(0.2,0.2,0.2)
    });
    loader.load(url, map => {
      material.map = map;
      map.wrapS = THREE.RepeatWrapping;
      map.wrapT = THREE.RepeatWrapping;
    });
    return material;

  }

  //创建玻璃材质 0xccddff  0x98F4FF
  createGlass(color = 0xffffff) {

    // var material = new THREE.MeshStandardMaterial({
    //     opacity: 0.25,
    //     premultipliedAlpha: true,
    //     transparent: true,
    //     color: color,
    //     refractionRatio: 1.5,
    // });




    // var material = new THREE.MeshPhysicalMaterial({
    //   map: null,
    //   color: color,
    //   metalness: 0.3,
    //   roughness: 0.5,
    //   transmission: 0.9,
    //   transparent: true
    // });




    var material = new THREE.MeshPhongMaterial({
      // color: new THREE.Color(0.13725490196078433,0.5803921568627451,0.8),
      color: color,
      transparent: true,
      opacity:0.25,
      side:THREE.DoubleSide,
      depthWrite:false,
      specular:new THREE.Color(0.8627451,0.8627451,0.3137255),
    });
    return material;
  }

  //创建单色材质
  createColor(color = 0xF5F5F5) {
    let meaterial = new THREE.MeshPhongMaterial({
      color: color,
      side: 2
    });
    return meaterial;
  }

  //旧版本创建贴图
  createTexture(url) {
    let loader = new THREE.TextureLoader();
    let material = new THREE.MeshBasicMaterial({
      side: 2
    });
    loader.load(url, map => {
      material.map = map;
      map.wrapS = THREE.RepeatWrapping;
      map.wrapT = THREE.RepeatWrapping;
      console.log("map:",map);
    });
    return material;
  }


  //新版本处理url,返回多个promise对象
  loadAsync(MaterialNodes) {
    let TexturePromiseArr = [];
    for (var index in MaterialNodes) {
      var materialNode = MaterialNodes[index];
      if (materialNode.TextureUrl !== null) {
        console.log('有贴图的材质创建:');
        // console.log(materialNode.TextureUrl)
        const texturePromise = new Promise((resolve, reject) => {
          let loader = new THREE.TextureLoader();
          loader.loadAsync(materialNode.TextureUrl).then(texture => {
            resolve(texture);
          }).catch(error => {
            console.log('materialNode.TextureUrl' + error);
            console.log(materialNode.TextureUrl);
            reject(error);
          })
        });
        TexturePromiseArr.push(texturePromise)
        this.haveTextureMaterialNode.push(materialNode)
      }
      else {
        console.log('无贴图的材质创建:');
        this.createMaterialBySingle(materialNode, null);
      }
    }
    return Promise.all(TexturePromiseArr);
  }

  //贴图请求完之后创建材质
  createMaterialBySingle(materialNode, map) {

    let material = null;
    switch (materialNode.MaterialTp) {
      case 0:
        //玻璃
        material = this.createGlass(this.parseColor(materialNode.ColorJson))
        break;
      case 1:
        //贴图
        material = this.createTextureByObject(map);
        break;
      case 2:
        //纯色
        material = this.createColor(this.parseColor(materialNode.ColorJson));
        break;
      default:
        break;
    }
    this.mapMaterial(materialNode.AttachIDS, material);
  }

  //已经拿到贴图创建材质
  createTextureByObject(map) {
    let material = new THREE.MeshBasicMaterial({
      side: 2
    });
    material.map = map;
    map.wrapS = THREE.RepeatWrapping;
    map.wrapT = THREE.RepeatWrapping;
    return material;
  }


  //获取有贴图的材质节点
  getHaveTextureMaterialNodes() {
    return this.haveTextureMaterialNode;
  }

}

export default MaterialMgr;
